<?php

use com\gdl\kq\interfaces\PlatformSettingServiceIf;
use com\gdl\common\dataobject\common\ResultDO;
use com\gdl\kq\dataobject\platformsetting\platformsettingDTO;


class api_PlatformSettingService extends api_Abstract implements PlatformSettingServiceIf
{
	
	//平台配置列表
	public function getPlatformSetting(platformsettingDTO $platformsettingDTO)
    {
		$result = new ResultDO();
		$data      = $platformsettingDTO->data ? $platformsettingDTO->data : [];
		$id        = $platformsettingDTO->id ? $platformsettingDTO->id : 0;//平台配置ID
		$key        = $platformsettingDTO->key ? $platformsettingDTO->key : '';//平台配置键名
		$platformId = $platformsettingDTO->platformId ? $platformsettingDTO->platformId : 0;//平台ID

				
	    try {
//	        $adminReg = $data['adminReg'];
//	        $adminUserId = $data['adminUserId'];
            $condition = [];
                if ($id){
                    $condition['id'] = $id;
                    if ($platformId){
                        $condition['platformId'] = $platformId;
                    }
                    if ($key){
                        $condition['key'] = $key;
                    }
                    $platformsettingModel = new model_attendance_platformsetting();
                    $res = $platformsettingModel->select($condition)->items;
                    $result->data = $res;
                    if(!empty($res[0])){
                        $result->code = 1;
                    } else {
                        $result->code = 0;
                    }

                    $result->success = true;
                }else{
                    $condition['platformId'] = $platformId;
                    if ($key){
                        $condition['key'] = $key;
                    }
                    $platformsettingModel = new model_attendance_platformsetting();
                    $res = $platformsettingModel->select($condition)->items;
                    $result->data = $res;
                    if(!empty($res[0])){
                        $result->code = 1;
                    } else {
                        $result->code = 0;
                    }

                    $result->success = true;
                }
            } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage();
            }
            return $result;
	}


    //添加平台配置
    public function addPlatformSetting(platformsettingDTO $platformsettingDTO)
    {
        $result = new ResultDO();
        $data      = $platformsettingDTO->data ? $platformsettingDTO->data : [];
        $platformId = $platformsettingDTO->platformId ? $platformsettingDTO->platformId : 0;//平台ID


        try {
            $platformsettingModel = new model_attendance_platformsetting();
            //查询平台配置名是否重复
                $name = $platformsettingModel->selectOne(['platformId'=>$platformId,'name'=>$data['name']],'id');
                if ($name){
                    $result->code = 0;
                    $result->message = "平台配置名:（{$name}）重复，请换一个方案名！";
                }else{
                    $id = $platformsettingModel->insert($data);
                    $result->data = [[$id]];
                    if(!empty($id)){
                        $result->code = 1;
                        $result->message = "方案添加成功！";
                    } else {
                        $result->code = 0;
                        $result->message = "方案添加失败！";
                    }
                }
            $result->success = true;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }

    //修改平台配置
    public function editPlatformSetting(platformsettingDTO $platformsettingDTO)
    {
        $result = new ResultDO();
        $data      = $platformsettingDTO->data ? $platformsettingDTO->data : [];
        $id      = $platformsettingDTO->id ? $platformsettingDTO->id : 0;
        $platformId = $platformsettingDTO->platformId ? $platformsettingDTO->platformId : 0;//平台ID


        try {
            $platformsettingModel = new model_attendance_platformsetting();
            $platformsettingModel->update(" id={$id} ",$data);
            $result->data = [[$id]];
            if(!empty($id)){
                $result->code = 1;
                $result->message = "方案修改成功！";
            } else {
                $result->code = 0;
                $result->message = "方案修改失败！";
            }
            $result->success = true;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }

}

